<?php

use yii\helpers\Html;
use yii\grid\GridView;
use yii\widgets\Pjax;
use yii\grid\CheckboxColumn;
use yii\helpers\Url;
use yii\bootstrap\ActiveForm;
use yii\helpers\ArrayHelper;
/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', 'Achievements');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="achievement-index">
    <section id="input-wrapper">
        <div class="container">
            <div id="input-box" >
                <p class="achievement-title">
                    <i class="glyphicon glyphicon-search"></i>&nbsp;成绩查询
                </p>
                <div class="row">

                    <?php $form = ActiveForm::begin(); ?>
                    <?php $type = ['1'=>'是','0'=>'否'] ?>

                    <div class="col-md-6 col-lg-4">
                        <?= $form->field($model, 'full_name')->textInput(['class'=>'form-control','placeholder'=>"按考生姓名查询"])->label(false) ?>
                    </div>
                    <div class="col-md-6 col-lg-4">
                        <?= $form->field($model, 'id_card')->textInput(['class'=>'form-control','placeholder'=>"按考生身份证查询"])->label(false) ?>
                    </div>
                    <div class="col-md-6 col-lg-4">
                        <?= $form->field($model, 'mobile')->textInput(['class'=>'form-control','placeholder'=>"按电话号码查询"])->label(false) ?>
                    </div>
                    <div class="col-md-6 col-lg-4">
                        <?= $form->field($model, 'subject')->textInput(['class'=>'form-control','placeholder'=>"按考试科目查询"])->label(false) ?>
                    </div>

                    <div class="col-md-6 col-lg-4" >
                        <?= Html::submitButton(  Yii::t('app', '查找'), ['class' => 'btn btn-info']) ?>
                    </div>


                    <?php ActiveForm::end(); ?>
                </div>
            </div>
            <div >
                <p class="achievement-title"><i class="glyphicon glyphicon-align-right"></i>&nbsp;成绩名单</p>
                <div id="table-box">
                    <?php
                    $columns = [
                        ['class' => 'yii\grid\SerialColumn'],
                        [
                            "class" => "yii\grid\CheckboxColumn",
                            "name" => "id",
                            'headerOptions' => ['width'=>'30'],
                        ],
                        'full_name',
                        'subject',
                        'mobile',
                        'id_card',
                        'score',
                        [
                            "attribute" => "pass",
                            'value'=>function ($data){
                                return $data->pass == 1 ? '合格' : '不合格';
                            }
                        ],
                    ];
                     if (!Yii::$app->user->isGuest) {
                         $columns[] =  [
                             'class' => 'yii\grid\ActionColumn',
                             'template' => '{update}{delete}'
                         ];
                     }
                    ?>
                    <?php Pjax::begin(['id'=>'grid']); ?>
                    <?= GridView::widget([
                        'dataProvider' => $dataProvider,
                        'summary' => "",
                        "options" => ["class" => "grid-view","style"=>"overflow:auto", "id" => "grid"],
                        'columns' => $columns,
                    ]);
                    $this->registerJs("
$(document).on('click', '.gridview', function () {
        var keys = $('#grid').yiiGridView('getSelectedRows');
    });
");
                    ?>
                    <?php Pjax::end(); ?>
                    <?php if (!Yii::$app->user->isGuest) {?>
                        <?= Html::a("批量删除", "javascript:void(0);", ["class" => "btn btn-danger gridview",'onclick'=>'GetCheckbox()']) ?>
                    <?php }?>
                </div>
            </div>
        </div>
    </section>

</div>

<script type="text/javascript">
    /*<![CDATA[*/
    var GetCheckbox = function (){
        var data = new Array();
        $('input[name="id[]"]:checked').each(function (){
            data.push($(this).val());
        });
//        console.log(data);


        if(data.length > 0){
            $.post(
                '<?php echo Url::to(['/achievement/deleteall/']);?>',
                {'id':data},
                function (data) {
                var ret = $.parseJSON(data);
                console.log(ret);
                if (ret) {
                    $.pjax.reload("#grid");
//                    $.fn.yiiGridView.update('grid');
//                    $.pjax.reload({container:"#grid", async:false});
                }
            });
        }else{
            alert("请选择要删除的成绩!");
        }
    }
</script>